import breaDcrumb from '@/components/breadcrumb';
import set from '../components/set.vue';
import binding from '../components/binding.vue';
import { permissionsCompanyList } from "@/api/common.js";
//import frequency from '../components/frequency.vue';
import {
  productqueryPage,
  getAcceptanceCriteria,
  saveOrUpdAcceptanceCriteria
} from "@/api/smart/specialmaterial/quality.js";
import mixin from "@/mixins/ICPSmixin.js";
export default {
  components: {
    breaDcrumb,
    set,
    binding,
    //  frequency
  },
  mixins: [mixin],
  data() {
    return {
      companylist: [],
      contractTypelist: [{
        value: '验收标准',
        id: 0
      }, {
        value: '参数设置',
        id: 1
      },
      {
        value: '物料检测绑定',
        id: 2
      },],
      currenindex: 0,
      leftList: [{
        text: '特材原料',
        value: 1
      }, {
        text: '特材成品',
        value: 2
      }],
      leftitemList: [],
      showrightitemList: [],
      rightitemList: [],
      tablenav: [],
      categoryList: [
        { name: '特材原料检验', id: 1 }, { name: '特材原料复检', id: 2 }, { name: '特材成品入库检验', id: 3 }, { name: '特材成品复检', id: 4 },
      ],
      companyId: '',
      companyName: '',
      companyIdList: [],
      showleftIndex: 1,
      leftIndex: 1,
      leftitemId: '',
      showleftitemId: '',
      showrightIndex: 0,
      rightIndex: 1,
      loading: false,
      /**判断是否是交工数据*/
      signData: false,
    };
  },
  async created() {
    this.companylist = await permissionsCompanyList();
    this.companyId = this.companylist[0].id
    console.log(this.companylist);
    this.initQualityType()
  },
  watch: {
    /*监听左侧导航获取不同的左侧列表*/
    currenindex() {
      console.log('切换tabel');
      let _this = this
      if (_this.leftitemId == '') {
        return;
      }
      _this.gettightList()
    },
    leftIndex: {
      handler() {
        this.initQualityType()
      },
      deep: false, //是否深度监听，true为是，默认为false
    },
    /*监听物料或者强度等级变化后获取右侧导航*/
    leftitemId() {
      console.log('数据');
      let _this = this
      if (_this.leftitemId == '') {
        return;
      }
      _this.gettightList()
    },
    companyId() {
      if (this.leftitemId == '') {
        return;
      }
      this.gettightList()
      this.initQualityType()
    },
    /*右侧数据裱花刷新表单*/
    rightIndex() {
      this.gettightList()
    },
  },
  methods: {
    /**
     * @Description: 获取原物料或者成品
     * @param {*}
     * @Author: wangwangwang
     */
    initQualityType() {
      if (!this.companyId) {
        return
      }
      productqueryPage({ dataType: this.leftIndex, page: 1, pageSize: 9999, companyId: this.companyId }).then((res) => {
        this.leftitemList = res.data.data.map(res => {
          let obj = {
            ...res,
            id: res.id,
            name: res.productName,
          }
          return obj
        });
        console.log(JSON.parse(JSON.stringify(this.leftitemList)))
        this.leftitemId = ''
        this.showleftitemId = ''
        this.rightitemList = []
        this.showrightitemList = []
        this.tablenav = []
        this.showrightIndex = 0
        this.rightIndex = this.leftIndex === 1 ? 1 : 3
      });
    },
    /**验收标准-列表 */
    gettightList() {
      if (this.showleftitemId) {
        let _this = this
        let data = new Object();
        data.typeId = _this.rightIndex;
        data.companyId = _this.companyId;
        if (_this.leftIndex == 1) {
          data.rawId = _this.showleftitemId;//原料
          data.productId = ''
        } else {
          data.productId = _this.showleftitemId;//成品
          data.rawId = ''
        }
        getAcceptanceCriteria(data).then(res => {
          this.tablenav = []
          this.rightitemList = []
          this.showrightitemList = []
          this.loading = false
          if (res.data && res.data?.claim?.length > 0 && res.data?.list?.length > 0) {
            this.tablenav = res.data.claim
            this.rightitemList = this.copy(res.data.list)
            this.showrightitemList = this.copy(res.data.list)
            this.tablenav.unshift({
              name: "检测类别"
            })
          }
        })
      }
    },
    copy(list) {
      let str = JSON.stringify(list)
      return JSON.parse(str)
    },
    /**保存数据*/
    addnbd() {
      let _this = this
      /*构造数据对象*/
      function setdata(content, projectId, requireId) {
        this.typeId = _this.rightIndex;
        this.companyId = _this.companyId;
        this.content = content;
        this.projectId = projectId;
        this.requireId = requireId;
        if (_this.leftIndex == 1) {
          this.rawId = _this.leftitemId;
        } else {
          this.productId = _this.leftitemId;
        }
      }
      let arr = []
      try {
        this.rightitemList.forEach(res => {
          for (let key in res) {
            if (key !== '0') {
              arr.push(new setdata(res[key]?.content, res[key]?.itemsId, res[key]?.claimId))
              if (res[key]?.type == 3 && res[key]?.symbol.length !== 1) {
                if (this.judge(res[key]?.content)) {
                  throw '警告，请输入正确的数字区间，如：10,20';
                }
              } else if (res[key]?.type == 3) {
                if (this.judge1(res[key]?.content)) {
                  console.log(res[key]);
                  console.log(res[key]?.content);
                  throw '警告，请输入正确的数字';
                }
              }
            }
          }
        })
      } catch (err) {
        console.log(err);
        this.$message({
          message: err,
          type: 'warning'
        });
        return;
      }
      console.log(JSON.parse(JSON.stringify(this.rightitemList)))
      console.log(JSON.parse(JSON.stringify(this.tablenav)))
      console.log(JSON.parse(JSON.stringify(arr)))
      saveOrUpdAcceptanceCriteria(arr).then((res) => {
        if (res.code == 1001) {
          this.showrightitemList = JSON.parse(JSON.stringify(this.rightitemList))
          this.$message({
            message: '保存成功',
            type: 'success'
          });
        }
      })

    },
    judge(str) {
      let re = new RegExp(/^[0-9]+([.]{1}[0-9]+){0,1},[0-9]+([.]{1}[0-9]+){0,1}$/)
      return !re.test(str)
    },
    judge1(str) {
      let re = new RegExp(/^[0-9]+([.]{1}[0-9]+){0,1}/)
      return !re.test(str)
    },
    change(hid, show, value) {
      let _this = this;
      // if (JSON.stringify(_this.showrightitemList) !== JSON.stringify(_this.rightitemList)) {
      //   this.$confirm('请保存更改数据,否则数据将会丢失?', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     cancelButtonClass: 'bottom_all_two',
      //     confirmButtonClass: 'bottom_all',
      //     type: 'warning'
      //   })
      //     .then(() => {
      //       _this.addnbd();
      //       _this[show] = _this[hid]
      //     })
      //     .catch(() => {
      //       _this[show] = value;
      //       _this[hid] = value;
      //     });
      //   return;
      // }
      _this[show] = value;
      _this[hid] = value;
    },
    choose(num) {
      this.currenindex = num
    },
  }
};